package com.erp.erp_servers.log.imp;

import com.erp.erp_dao.LogDB.ILogDBDao;
import com.erp.erp_entitys.LogDB.LogDBEntity;
import com.erp.erp_servers.log.ILogDBService;
import com.erp.erp_servers.log.LogWriteDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Service
public class LogDBServiceImp implements ILogDBService {
    @Autowired
    private  ILogDBDao logDBDao;


    @Override
    public List<LogDBEntity> query(LogDBEntity logDBEntity) {
        return logDBDao.query(logDBEntity);
    }

    /**
     *
     * @param tf=true:表示解决今天的；false：表示昨天的
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(boolean tf ) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String fileName="";
        if(tf){
            String rq=sdf.format(new Date());
            fileName=rq+"_log.txt";
            //如果时解析当天，则先删除当彰已解析的数据
             logDBDao.delete();
        }else{
            Calendar cal   =   Calendar.getInstance();
            cal.add(Calendar.DATE,   -1);
            String yesterday = sdf.format(cal.getTime());
            System.out.println(yesterday);
            fileName=yesterday+"_log.txt";
        }
        LogWriteDB log=new LogWriteDB();
        List<LogDBEntity> logDBEntities = log.readFileContent(fileName);
        int num=0;
        for(int i=0;i<logDBEntities.size();i++){
           num= logDBDao.insert(logDBEntities.get(i));
        }
        return num;
    }

    @Override
    public int insert(LogDBEntity logDBEntity) {
        return logDBDao.insert(logDBEntity);
    }
}
